/**
 * 
 */
package filePackets;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * This Packet type is used to transfer data between the client and the server.
 * @author peleg
 */
public class DataPacket extends FilePacket {

	public DataPacket(byte[] ba) throws IOException, ClassNotFoundException {
		super(ba);
		ByteArrayInputStream bis = new ByteArrayInputStream(ba);
		ObjectInputStream ois = new ObjectInputStream(bis);
		DataPacket obj = (DataPacket)ois.readObject();
		bis.close();
		ois.close();
		this.setBlockData(obj.getBlockData());
		this.setBlockNumber(obj.getBlockNumber());		
	}

	public DataPacket() {
		super();
	}

	private static final long serialVersionUID = 3861083651379157435L;

	public PacketType getPacketType() {
		return FilePacket.PacketType.DATA;
	}
	
	// Data fields
	Integer BlockNumber;
	byte[] BlockData;

	public Integer getBlockNumber() {
		return BlockNumber;
	}
	public void setBlockNumber(Integer blockNumber) {
		BlockNumber = blockNumber;
	}
	public byte[] getBlockData() {
		return BlockData;
	}
	public void setBlockData(byte[] blockData) {
		BlockData = blockData;
	}
	@Override
	public byte[] toByteArray() throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(this);
		oos.flush();
		oos.close();
		bos.close();
		byte [] data = bos.toByteArray();
		return data;
	}

}
